package com.maila.calltaxi_driver.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.maila.calltaxi_driver.R;
import com.maila.calltaxi_driver.utils.EventData;
import com.zhy.autolayout.utils.AutoUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by Administrator on 2017/7/4.
 */

public class DaulPickerPop extends PopupWindow {

    private View mMenuView;
    TextView cancle,confirm;
    private TimePicker startime,endtime;
    boolean startok;
    boolean endok;
    String start,end="";
    public DaulPickerPop(final Context context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.daul_timepicker, null);
        AutoUtils.autoSize(mMenuView);
        cancle= (TextView) mMenuView.findViewById(R.id.btn_cancel);
        confirm= (TextView) mMenuView.findViewById(R.id.btn_confirm);
        startime=(TimePicker) mMenuView.findViewById(R.id.tp_start);
        endtime=(TimePicker) mMenuView.findViewById(R.id.tp_end);
        startime.setIs24HourView(true);
        endtime.setIs24HourView(true);
        startime.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                startok=true;
                if(minute<10){
                    start=hourOfDay+":0"+minute;
                }else {
                    start=hourOfDay+":"+minute;
                }
            }
        });
        endtime.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                endok=true;
                if(minute<10){
                    end=hourOfDay+":0"+minute;
                }else {
                    end=hourOfDay+":"+minute;
                }

            }
        });

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(startok&&endok){
                    EventBus.getDefault().post(new EventData(EventData.EVENT_TYPE_SELECTE_TIME,start+"-"+end));
                    dismiss();
                }else {
                    Toast.makeText(context,"请完善开工时间和收工时间",Toast.LENGTH_SHORT).show();
                }

            }
        });
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
//            设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
//            设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.sharepopupanimation);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(80000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                return true;
            }
        });
    }
}
